package io.lkt.modules.sys.controller;

import java.io.IOException;
import java.util.*;

import io.lkt.common.util.Query;
import io.lkt.common.utils.AliyunOssClientUtil;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BBrandInfoEntity;
import io.lkt.modules.sys.entity.SysUserEntity;
import io.lkt.modules.sys.service.BBrandInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.PythonArticleInfoEntity;
import io.lkt.modules.sys.service.PythonArticleInfoService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 爬虫待入库文章
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2019-03-23 16:55:35
 */
@RestController
@RequestMapping("/sys/pythonarticleinfo")
public class PythonArticleInfoController extends AbstractController{
    @Autowired
    private PythonArticleInfoService pythonArticleInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:pythonarticleinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        BBrandInfoController brandInfoController=new BBrandInfoController();
        PageUtils page = pythonArticleInfoService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @RequestMapping("/queryList")
    @RequiresPermissions("sys:pythonarticleinfo:list")
    public R queryList(@RequestParam Map<String, Object> params){
        Query query = new Query(params);
        List<PythonArticleInfoEntity> list=pythonArticleInfoService.queryList(query);
        int total = pythonArticleInfoService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page",pageUtil);
    }

    /**
     *
     * @param params
     * @return
     */
    @RequestMapping("/elseList")
    @RequiresPermissions("sys:pythonarticleinfo:list")
    public R elseList(@RequestParam Map<String, Object> params){
        Query query = new Query(params);
        List<PythonArticleInfoEntity> list=pythonArticleInfoService.queryList(query);
        int total = pythonArticleInfoService.elseTotal(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page",pageUtil);
    }

    /**
     * 品牌下拉框
     * @return
     */
    @RequestMapping("/dataList")
    @RequiresPermissions("sys:pythonarticleinfo:list")
    public R dataList(){
        Map<String,Object> map=new HashMap<>();
        List<PythonArticleInfoEntity> dataList=pythonArticleInfoService.byBrandName(map);
        return R.ok().put("dataList",dataList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:pythonarticleinfo:info")
    public R info(@PathVariable("id") Integer id){
        PythonArticleInfoEntity bArticleInfo = pythonArticleInfoService.getInfo(id);
        if(bArticleInfo.getArticleThumbnail()!=null && !bArticleInfo.getArticleThumbnail().contains("zktfile.wxslzf.com")) {
            String articleImgUrl = AliyunOssClientUtil.uploadFileByFileUrl2(bArticleInfo.getArticleThumbnail());
            bArticleInfo.setArticleThumbnail(articleImgUrl);
            pythonArticleInfoService.updateArticleThumbnail(id,bArticleInfo.getArticleThumbnail());

        }

        return R.ok().put("bArticleInfo", bArticleInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:pythonarticleinfo:save")
    public R save(@RequestBody PythonArticleInfoEntity bArticleInfo){
        pythonArticleInfoService.save(bArticleInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:pythonarticleinfo:update")
    public R update(@RequestBody PythonArticleInfoEntity bArticleInfo){
        ValidatorUtils.validateEntity(bArticleInfo);
        pythonArticleInfoService.updateById(bArticleInfo);
        
        return R.ok();
    }

    @RequestMapping("/updateInfo")
    @RequiresPermissions("sys:pythonarticleinfo:update")
    public R updateInfo (@RequestBody PythonArticleInfoEntity bArticleInfo) throws IOException {
        ValidatorUtils.validateEntity(bArticleInfo);
        SysUserEntity curUser= getUser();
        bArticleInfo.setUpdateUser(curUser.getUsername());
        String articleContent = updateImgs(bArticleInfo.getArticleContent());
        bArticleInfo.setArticleContent(articleContent);
        pythonArticleInfoService.updateInfo(bArticleInfo);

        return R.ok();
    }

    //把文章中所有的图片上传到oss，并替换路径
    private String updateImgs(String articleContent) throws IOException {
        Document document = null;
        document = Jsoup.parse(articleContent);

        Elements imgElements =document.select("img");

        for (Element img : imgElements) {
            String src="";
            if(img.attr("src")!=null && !img.attr("src").contains(AliyunOssClientUtil.ossImgUrl)) {
                src = AliyunOssClientUtil.uploadFileByFileUrl2(img.attr("src"));
                img.attr("src", src);
            }
        }
        return document.body().html();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/upDel")
    @RequiresPermissions("sys:pythonarticleinfo:update")
    public R upDel(@RequestBody PythonArticleInfoEntity bArticleInfo){
        HashMap<String,Object> map=new HashMap<>();
        map.put("id",bArticleInfo.getId());
        map.put("isDel",bArticleInfo.getIsDel());
        pythonArticleInfoService.upDel(map);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delById")
    @RequiresPermissions("sys:pythonarticleinfo:delete")
    public R delById(@RequestBody PythonArticleInfoEntity bArticleInfo){
        pythonArticleInfoService.delById(bArticleInfo.getId());

        return R.ok();
    }

}
